(function($, doc) {
	mui.init({});
	var userinfo = app.getState() || {};
	var uid = userinfo.user_id;
	window.onload = function() {

		mui.plusReady(function() {
			//关闭等待框
			plus.nativeUI.closeWaiting();
			//显示当前页面
			mui.currentWebview.show();
			var width = document.documentElement.clientWidth;
			var height = document.documentElement.clientHeight;
			document.getElementById('account_pwd').style.width = width + 'px';
			document.getElementById('account_pwd').style.height = height + 'px';

			var trade_id = plus.webview.currentWebview().trade_id;
			mui.ajax({
				url: apiurl + "Trade/get_trade/",
				data: {
					'trade_id': trade_id,
					'_token': userinfo.token
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status == -3) {
						mui.toast(res.msg);
						app.out();
						return false;
					}

					// 请求成功
					//res.evidence=serverurl + 'Uploads/trade/' + trade_id + '.jpg';
					if(res.data.img.indexOf(upurl + 'Uploads') !== -1) {
						res.data.evidence = res.data.img;
					} else {
						res.data.evidence = upurl + res.data.img;
					}

					doc.querySelector("#main-data").innerHTML = template('main-template', res.data);
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});

			doc.getElementById('do_post').addEventListener('tap', function() {
				document.getElementById('account_pwd').style.display = "block";
				document.getElementById('main_content').style.display = "block";
				document.getElementById('keyboard').style.display = "block";
				var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
				mui.each(pwd_array, function(index, item) {
					if(item.value != '') {
						item.value = '';
					}
				});
			})

			doc.getElementById('close_win').addEventListener('tap', function() {
				document.getElementById('account_pwd').style.display = "none";
				document.getElementById('main_content').style.display = "none";
				document.getElementById('keyboard').style.display = "none";
			})

			//点击数字
			mui('#keyboard').on('tap', '.num.data_num', function() {
				var number = this.getAttribute('data-num');
				var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
				var f = false;
				var pwd = '';
				mui.each(pwd_array, function(index, item) {
					if(item.value == '') {
						item.value = number;
						return false;
					}
				});

				mui.each(pwd_array, function(index, item) {
					if(item.value == '') {
						f = true;
						return false;
					}
					pwd += item.value;
				});

				if(!f) {
					//mui.alert('正在后台处理打款...');
					var Info = {
						user_id: userinfo.user_id,
						trade_pwd: pwd,
						tid: trade_id,
						_token: userinfo.token
					}
					document.getElementById('account_pwd').style.display = "none";
					document.getElementById('main_content').style.display = "none";
					document.getElementById('keyboard').style.display = "none";
					plus.nativeUI.showWaiting();
					mui.ajax({
						url: apiurl + "Trade/do_receipt/",
						data: Info,
						async: true,
						dataType: 'json',
						crossDomain: true, //强制使用5+跨域
						type: 'post',
						timeout: 10000,
						success: function(res) {
							console.log(JSON.stringify(res));
							plus.nativeUI.closeWaiting();
							if(res.status == -3) {
								mui.toast(res.msg);
								app.out();
								return false;
							}
							// 请求成功
							if(res.status < 1) {
								mui.alert(res.msg);
							} else {
								mui.toast('确认收款成功');
								mui.fire(plus.webview.getWebviewById('sell_remit.html'), 'refresh_data');
								mui.fire(plus.webview.getWebviewById('sell_uncompleted.html'), 'refresh_data');
								mui.back();
							}
						},
						error: function(xhr, type, errorThrown) {
							//console.log(JSON.stringify(xhr));
							//console.log(JSON.stringify(type));
							//console.log(JSON.stringify(errorThrown));
							mui.toast('操作失败！');
						}
					});
				}

			})

			//点击删除
			mui('#keyboard').on('tap', '#numC', function() {
				var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
				var len = pwd_array.length;
				for(i = len - 1; i >= 0; i--) {
					if(pwd_array[i].value != '') {
						pwd_array[i].value = '';
						break;
					}
				}
			});

		});

	}

}(mui, document));

//联系他
function do_tel(phone) {
	if(mui.os.plus) {
		plus.device.dial(phone);
	} else {
		location.href = 'tel:' + phone;
	}
}